package com.spring_hibernate;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class BookDaoImpl implements BookDao{
	private SessionFactory sessionFactory;
	    
    //获取和当前线程绑定的Seesion
    private Session getSession()
    {
    	sessionFactory=(SessionFactory) BookDaoImplTest.context.getBean("sessionFactory");
    	return sessionFactory.openSession();
    }
    public String findBookById(int id)
    {
        String hql="from Book where id=?";
        Query query=getSession().createQuery(hql).setInteger(0, id);
        Book book= (Book) query.uniqueResult();
        return book.toString();
    }
    public void saveBook(Book book)
    { 	
    	System.out.println(book);
    	getSession().save(book);
    }
}
